/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @ActBaList.java  2014-9-10 下午4:30:23 - Carson
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.act;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaListActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.entity.BarModel;
import com.android.tiango.baba.entity.BaseEntity;
import com.android.tiango.baba.entity.SimpleTopicEntity;
import com.android.tiango.baba.entity.SimpleTopicModel;
import com.android.tiango.baba.entity.SimpleUserModel;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.TimeToUtil;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.BaListPopWindow;
import com.android.tiango.baba.view.BaListPopWindow.PopWindowsClickListener;
import com.android.tiango.baba.view.PullListView.PullListViewListener;
import com.android.tiango.baba.view.RoundImageView;

/**
 * 贴吧列表
 * @author YW_pc
 * 
 */
public class ActBaList extends BaListActivity<SimpleTopicModel> implements
		com.android.tiango.baba.BaListActivity.AddHeaderViewCallBack, PullListViewListener,
		PopWindowsClickListener {
	private BaListPopWindow baPopWindow;
	private BarModel barModel;
	private RoundImageView iv_head;

	private TextView tv_name;
	private TextView tv_num;
	private TextView tv_huati;
	private TextView tv_content;
	private TextView tv_jh;
	private TextView tv_jhoin;
	private String filterType = filterType_0;
	private static final int JOIN_BAR = 1;
	private static final int EXIT_BAR = 0;

	private static final String filterType_0 = "0";// 0-默认排序
	private static final String filterType_1 = "1";// 1-本吧精华
	private static final String filterType_2 = "2";// 2-我发的贴
	private static final String filterType_3 = "3";// 3-我回的贴

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setHeaderView(this);
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		if (intent.hasExtra(Constants.BARID)) {
			barModel = (BarModel) intent.getSerializableExtra(Constants.BARID);
		} else {
			finish();
		}
		initHeader();
		if (null != mTitleBar) {
			mTitleBar.setTitle(barModel.getBarName());
			mTitleBar.setImageBackGround(R.drawable.bg_title_blue);
			mTitleBar.setBackIcon(R.drawable.icon_back);
			mTitleBar.setMoreTextBg(R.drawable.icon_titleright2);
			mTitleBar.setMoreImageOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					baPopWindow = new BaListPopWindow(ActBaList.this);
					baPopWindow.setPopWindowsClickListener(ActBaList.this);
					ArrayList<String> list = new ArrayList<String>();
					list.add("我发的贴");
					list.add("我回的贴");
					if (barModel.getIsJoin() == 1) {
						list.add("退出吧");
					}
					baPopWindow.setData(list);
					baPopWindow.showPopWindow(mTitleBar.mImageMore);
				}
			});
		}
		setBackOnClickListener();
		mListView.setPullListener(this);
		mListView.startOnRefresh();
	}

	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(this).inflate(R.layout.item_actpost, null);
			holder.tv_acttitle = (TextView) convertView.findViewById(R.id.tv_acttitle);
			holder.tv_actinfo = (TextView) convertView.findViewById(R.id.tv_actinfo);

			holder.iv_acthead = (RoundImageView) convertView.findViewById(R.id.iv_acthead);
			holder.tv_acttitle = (TextView) convertView.findViewById(R.id.tv_acttitle);
			holder.tv_actinfo = (TextView) convertView.findViewById(R.id.tv_actinfo);
			holder.tv_acttime = (TextView) convertView.findViewById(R.id.tv_acttime);
			holder.iv_sex = (ImageView) convertView.findViewById(R.id.iv_sex);
			holder.iv_thelord = (ImageView) convertView.findViewById(R.id.iv_thelord);

			holder.tv_actcomments = (TextView) convertView.findViewById(R.id.tv_actcomments);
			holder.tv_acttoview = (TextView) convertView.findViewById(R.id.tv_acttoview);

			holder.iv_actre = (ImageView) convertView.findViewById(R.id.iv_actre);
			holder.iv_actj = (ImageView) convertView.findViewById(R.id.iv_actj);
			holder.iv_actd = (ImageView) convertView.findViewById(R.id.iv_actd);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final SimpleTopicModel bean = mListData.get(position);
		if (null != bean) {
			if (bean.getIsHot() == 1) {// 是否热点
				holder.iv_actre.setVisibility(View.VISIBLE);
			} else {
				holder.iv_actre.setVisibility(View.INVISIBLE);
			}
			if (bean.getIsTop() == 1) {// 是否指定
				holder.iv_actd.setVisibility(View.VISIBLE);
			} else {
				holder.iv_actd.setVisibility(View.INVISIBLE);
			}
			if (bean.getIsNice() == 1) {// 是否精
				holder.iv_actj.setVisibility(View.VISIBLE);
			} else {
				holder.iv_actj.setVisibility(View.INVISIBLE);
			}
			if (bean.getIsBarOwner() == 1) {// 是吧主
				holder.iv_thelord.setVisibility(View.VISIBLE);
			} else {
				holder.iv_thelord.setVisibility(View.INVISIBLE);
			}

			SimpleUserModel userBean = bean.getSimpleUser();
			if (null != userBean) {
				mImageLoader.loadImage(userBean.getAvatarUrl(), holder.iv_acthead);
				holder.tv_acttitle.setText(userBean.getNickName());
				if (userBean.getGender() == 1) {
					holder.iv_sex.setBackgroundResource(R.drawable.bg_sex);
				} else if (userBean.getGender() == 2) {
					holder.iv_sex.setBackgroundResource(R.drawable.bg_sex_girl);
				}
			} else {
				mImageLoader.loadImage("", holder.iv_acthead);
				holder.tv_acttitle.setText("");
			}
			holder.tv_actinfo.setText(bean.getTopicName());
			holder.tv_actcomments.setText(String.valueOf(bean.getReadNumber()));
			holder.tv_acttoview.setText(String.valueOf(bean.getReplyNumber()));
			holder.tv_acttime.setText(TimeToUtil.getSystemDayFormat(bean.getCreateTime()));

		}
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ActBaList.this, ActCardDetail.class);
				intent.putExtra(Constants.BARID, bean.getBarId());
				intent.putExtra(Constants.TOPICID, bean.getTopicId());
				intent.putExtra(Constants.BARNAME, bean.getBarName());
				startActivity(intent);
			}
		});
		return convertView;
	}

	class ViewHolder {
		private RoundImageView iv_acthead;
		private TextView tv_acttitle;
		private TextView tv_actinfo;
		private TextView tv_acttime;
		private ImageView iv_sex;
		private ImageView iv_thelord;
		private TextView tv_actcomments;
		private TextView tv_acttoview;

		private ImageView iv_actre;
		private ImageView iv_actj;
		private ImageView iv_actd;

	}

	@Override
	public void onPullRefresh() {
		loadData(filterType);

	}

	@Override
	public void onPullLoadMore() {
		loadData(filterType);

	}

	@Override
	public void addHeaderView() {
		View header = LayoutInflater.from(this).inflate(R.layout.header_bar, null);
		iv_head = (RoundImageView) header.findViewById(R.id.iv_head);
		tv_name = (TextView) header.findViewById(R.id.tv_name);
		tv_num = (TextView) header.findViewById(R.id.tv_num);
		tv_huati = (TextView) header.findViewById(R.id.tv_huati);
		tv_content = (TextView) header.findViewById(R.id.tv_content);
		tv_jh = (TextView) header.findViewById(R.id.tv_jh);
		tv_jhoin = (TextView) header.findViewById(R.id.tv_jhoin);
		mListView.addHeaderView(header);

		RelativeLayout in_public = (RelativeLayout) findViewById(R.id.in_public);
		in_public.setVisibility(View.VISIBLE);
		LinearLayout ll_send_card = (LinearLayout) findViewById(R.id.ll_send_card);
		ll_send_card.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// showActivity(ActPublic.class, false);
				Intent intent = new Intent(ActBaList.this, ActPublic.class);
				intent.putExtra(Constants.BARID, barModel.getBarId());
				ActBaList.this.startActivity(intent);
			}
		});
		tv_jh.setOnClickListener(new OnClickListener() {// 精华帖

			@Override
			public void onClick(View v) {
				// showActivity(ActPostList.class, false);
				Intent intent = new Intent(ActBaList.this, ActGoodsPostList.class);
				intent.putExtra(Constants.BARID, barModel.getBarId());
				ActBaList.this.startActivity(intent);
			}
		});

	}

	private void initHeader() {
		if (null != barModel) {
			tv_name.setText(barModel.getBarOwner());
			tv_num.setText(barModel.getMemberNumber());
			tv_huati.setText(String.valueOf(barModel.getTodayTopicNumber()));
			tv_content.setText(barModel.getBarSummary());
			if (barModel.getIsJoin() == 1) {
				tv_jhoin.setText("已加入");
				tv_jhoin.setBackgroundColor(getResources().getColor(R.color.color_light));
				tv_jhoin.setOnClickListener(null);
			} else {
				tv_jhoin.setText("加入吧");
				tv_jhoin.setBackgroundResource(R.drawable.selecter_bar);
				tv_jhoin.setOnClickListener(new OnClickListener() {// 加入吧

					@Override
					public void onClick(View v) {
						addBar(JOIN_BAR);
					}
				});
			}
		}
	}

	private void loadData(String filterType) {
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams param = new HttpParams();
		param.put("barId", barModel.getBarId());
		param.put("filterType", "0"); // 0-默认排序 1-本吧精华 2-我发的贴 3-我回的贴
		param.put("pageIndex", mPageIndex);
		param.put("pageSize", Constants.PAGE_SIZE);

		client.get(HttpUrl.getUrl(HttpUrl.FilterTopics), param, new HttpCallBack() {

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
				mListView.onRefreshFinish();
				mListView.onLoadFinish();
			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpSuccess(Object arg0) {
				mImageLoader.loadImage(barModel.getBarIcon(), iv_head);
				SimpleTopicEntity entity = (SimpleTopicEntity) arg0;
				ArrayList<SimpleTopicModel> data = entity.getData();
				if (null != data) {
					mAdapter.putData(data);
				}
				mListView.onRefreshFinish();
				mListView.onLoadFinish();
			}

		}, SimpleTopicEntity.class);//

	}

	@Override
	public void popOnclick(int position, String msg) {
		switch (position) {
		case 0:// 我发的帖子
			filterType = filterType_2;
			mListView.startOnRefresh();
			break;
		case 1:// 我回的帖子
			filterType = filterType_3;
			mListView.startOnRefresh();
			break;
		case 2:// 退出
			addBar(EXIT_BAR);
			break;
		default:
			break;
		}
	}

	// 加入/退出吧
	private void addBar(int type) {
		if (null == barModel) {
			return;
		}
		showWaitingDialog();
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams param = new HttpParams();
		param.put("barId", barModel.getBarId());
		param.put("token", ShareCookie.getToken());
		param.put("type", String.valueOf(type));// 0-退出 1-加入

		client.post(HttpUrl.getUrl(HttpUrl.OperateTopicBar), param, new HttpCallBack() {

			@Override
			public void onHttpSuccess(Object arg0) {
				dismissWaitingDialog();
				BaseEntity entity = (BaseEntity) arg0;
				if (null != entity) {
					ToastUtils.showMessage(entity.getMessage());
				}
			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
				dismissWaitingDialog();
			}
		}, BaseEntity.class);//
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if (arg1 == RESULT_OK) {
			mListView.startOnRefresh();
		}
	}
}
